//
//  CFRMTabBarController.swift
//  CrazyFatReducingMeal
//
//  Created by iOS-Czz on 2023/11/8.
//

import UIKit

class CFRMTabBarController: UITabBarController {
    
    private var collaborationVC: CFRMTodayRecipeController!
    private var cooperativeFactory: CFRMRecipeListController!
    private var licenseVC: CFRMDishesListController!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collaborationVC = CFRMTodayRecipeController()
        collaborationVC.title = "Today"
        collaborationVC.tabBarItem.selectedImage = UIImage(named: "first_s")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        collaborationVC.tabBarItem.image = UIImage(named: "first_n")?.withRenderingMode(.alwaysOriginal)
        
        cooperativeFactory = CFRMRecipeListController()
        cooperativeFactory.title = "Recipe"
        cooperativeFactory.tabBarItem.selectedImage = UIImage(named: "second_s")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        cooperativeFactory.tabBarItem.image = UIImage(named: "second_n")?.withRenderingMode(.alwaysOriginal)
        
        licenseVC = CFRMDishesListController()
        licenseVC.title = "Dishes"
        licenseVC.tabBarItem.selectedImage = UIImage(named: "third_s")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        licenseVC.tabBarItem.image = UIImage(named: "third_n")?.withRenderingMode(.alwaysOriginal)
        
        //设置默认文字颜色
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : TAB_NORMAL_TITLE_COLOR], for: .normal)
        //设置选中文字颜色
        
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : TAB_SELECT_TITLE_COLOR], for: .selected)
        
        let collaborationNav = CFRMNavigationController(rootViewController: collaborationVC)
        let cooperativeFactoryNav = CFRMNavigationController(rootViewController: cooperativeFactory)
        let licenseNav = CFRMNavigationController(rootViewController: licenseVC)
        viewControllers = [collaborationNav,cooperativeFactoryNav,licenseNav]
        
        self.tabBar.tintColor = THEM_COLOR
        self.tabBar.backgroundColor = TAB_BG_COLOR
        if #available(iOS 15.0, *) {
            let appearance = UITabBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = TAB_BG_COLOR
            tabBar.standardAppearance = appearance
            tabBar.scrollEdgeAppearance = appearance
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: TAB_SELECT_TITLE_COLOR]
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: TAB_NORMAL_TITLE_COLOR]
            tabBar.tintColor = THEM_COLOR
        }
        if #available(iOS 13.0, *) {
            // 在 iOS 13 及以上版本使用新的设置方式
            let appearance = UITabBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = TAB_BG_COLOR
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: TAB_SELECT_TITLE_COLOR]
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: TAB_NORMAL_TITLE_COLOR]
            tabBar.standardAppearance = appearance
            tabBar.tintColor = TAB_SELECT_TITLE_COLOR
        } else {
            // 在 iOS 13 以下版本使用旧的设置方式
            tabBar.barTintColor = THEM_COLOR
            tabBar.tintColor = THEM_COLOR
//            tabBar.selectedImageTintColor = TAB_SELECT_TITLE_COLOR
            tabBar.selectedItem?.setTitleTextAttributes([.foregroundColor: TAB_NORMAL_TITLE_COLOR], for: .normal)
            tabBar.selectedItem?.setTitleTextAttributes([.foregroundColor: TAB_SELECT_TITLE_COLOR], for: .selected)
        }
        
        // Do any additional setup after loading the view.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
}
